package com.qjz.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//import javax.xml.datatype.XMLGregorianCalendar;

//import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;

public abstract class DateUtil {
	/**
	 * yyyy-MM-dd
	 */
	public final static String DATEPATTERN = "yyyy-MM-dd";

	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public final static String DATAFULLPATTERN = DATEPATTERN + " HH:mm:ss";

	/**
	 * MM/dd/yyyy
	 */
	public final static String DATEPATTERN2 = "MM/dd/yyyy";

	/**
	 * yyyy-MM-dd HH:mm:a
	 */
	public final static String TIMEPATTERN = DATEPATTERN + " HH:MM a";

	/**
	 * turn date into string
	 */
	public static String formatDate(Date dDate, String sFormat) {
		String tmp = "";
		try {
			if (dDate != null) {
				SimpleDateFormat formatter = new SimpleDateFormat(sFormat);
				String dateString = formatter.format(dDate);
				tmp = dateString;
			} else
				tmp = "";
		} catch (Exception ex) {
			System.out.println("Date" + dDate + "to String is error");
		}
		return tmp;
	}

	/**
	 * turn the string with date format to date
	 * @throws ParseException 
	 * 时间格式化，异常全抛给webservice，
	 */
	public static Date strToDate(String s, String pattern) throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		Date date1 = null;
		//try {
		Date theDate = formatter.parse(s);
		Date date = theDate;
		return date;
		//} catch (Exception ex) {
		//	System.out.println("String " + s + " is error");
		//}
		//return date1;
	}

	/**
	 * judge if the year is leap year
	 */
	public static boolean isLeap(int year) {
		boolean div4 = year % 4 == 0;
		boolean div100 = year % 100 == 0;
		boolean div400 = year % 400 == 0;
		return div4 && (!div100 || div400);
	}

	/**
	 * get the week number for date
	 */
	public static int getWeek(Date vDate) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(vDate);
		int week = cal.get(3);
		return week;
	}

	/**
	 * get the week day for date
	 */
	public static int getWeekDay(Date d) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		return cal.get(7);
	}

	/**
	 * add many month to a date varible
	 */
	public static Date addMonth(Date dDate, int iNbMonth) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(dDate);
		int month = cal.get(2);
		month += iNbMonth;
		int year = month / 12;
		month %= 12;
		cal.set(2, month);
		if (year != 0) {
			int oldYear = cal.get(1);
			cal.set(1, year + oldYear);
		}
		return cal.getTime();
	}

	/**
	 * reduce many month to a date varible
	 */
	public static Date reduceMonth(Date dDate, int iNbMonth) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dDate);
		int month = cal.get(2);
		month = month - iNbMonth;
		int year = month / 12;
		month %= 12;
		cal.set(2, month);
		if (year != 0) {
			int oldYear = cal.get(1);
			cal.set(1, year + oldYear);
		}
		return cal.getTime();
	}

	/**
	 * add many day to a date varible
	 */
	public static Date addDay(Date dDate, long iNbDay) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dDate);
		cal.add(5, (int) iNbDay);
		Date result = cal.getTime();
		return result;
	}

	/**
	 * add many year to a date varible
	 */
	public static Date addYear(Date dDate, int iNbYear) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dDate);
		int oldYear = cal.get(1);
		cal.set(1, iNbYear + oldYear);
		return cal.getTime();

	}

	/**
	 * reduce many year to a date varible
	 */
	public static Date reduceYear(Date dDate, int iNbYear) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dDate);
		int oldYear = cal.get(1);
		cal.set(1, oldYear - iNbYear);
		return cal.getTime();
	}

	/**
	 * get the diffrence between two date
	 */
	public static long dateDiff(Date date1, Date date2) {
		long date1ms = date1.getTime();
		long date2ms = date2.getTime();
		return date2ms - date1ms;
	}
	
	/**
	 * get the month diffrence between two date
	 */
	public static int dateMonthDiff(Date date1, Date date2) {
		int ydiff = getYear(date2) - getYear(date1);
		int mdiff = getMonth(date2) - getMonth(date1);
		return (ydiff * 12 + mdiff);
	}

	/**
	 * get the year for now
	 */
	public static int getYear() {
		Calendar Cal = Calendar.getInstance();
		return Cal.get(Calendar.YEAR);
	}

	/**
	 * get the year for input date
	 */
	public static int getYear(Date inputDate) {
		Calendar Cal = Calendar.getInstance();
		Cal.setTime(inputDate);

		return Cal.get(Calendar.YEAR);
	}

	/**
	 * get the month for now
	 */
	public static int getMonth() {
		Calendar Cal = Calendar.getInstance();
		return (Cal.get(Calendar.MONTH) + 1);
	}

	/**
	 * get the month for input date
	 */
	public static int getMonth(Date inputDate) {
		Calendar Cal = Calendar.getInstance();
		Cal.setTime(inputDate);
		return (Cal.get(Calendar.MONTH) + 1);
	}

	/**
	 * get the day for now
	 */
	public static int getDay() {
		Calendar Cal = Calendar.getInstance();
		return Cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * get the day for input Date
	 */
	public static int getDay(Date inputDate) {
		Calendar Cal = Calendar.getInstance();
		Cal.setTime(inputDate);
		return Cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 取得下月的年月的表示字符串
	 * 
	 * @param yearMonth
	 *            (200704)
	 * @param iNbMonth
	 *            下几个月
	 * @return String
	 * @throws ParseException 
	 */
	public static String nextMonth(String yearMonth, int iNbMonth) throws ParseException {
		String pattern = "yyyyMM";
		Date date = strToDate(yearMonth, pattern);
		date = DateUtil.addMonth(date, iNbMonth);
		yearMonth = DateUtil.formatDate(date, pattern);

		return yearMonth;
	}

	/**
	 * 得到某季度开始的月份
	 * 
	 * @param numSeason
	 * @return String 月份
	 */
	public static String getFirstMonth(int numSeason) {
		int m = (numSeason - 1) * 3 + 1;
		String month = m + "";

		return month.length() > 1 ? month : "0" + month;
	}

	/**
	 * 某季度最后的月份
	 * 
	 * @param numSeason
	 * @return String月份
	 */
	public static String getLastMonth(int numSeason) {
		int m = numSeason * 3;
		String month = m + "";

		return month.length() > 1 ? month : "0" + month;
	}

	/*public static XMLGregorianCalendar getXMLDate(Date date) {
		if (date == null)
			return null;
		XMLGregorianCalendar cal = new XMLGregorianCalendarImpl();

		// GregorianCalendar calendar = new GregorianCalendar();
		 Calendar calendar = Calendar.getInstance();
//		GregorianCalendar calendar = new GregorianCalendar();
//
//		calendar.setTime(date);

		cal.setYear(calendar.get(Calendar.YEAR));
		cal.setMonth(calendar.get(Calendar.MONTH) + 1);
		cal.setDay(calendar.get(Calendar.DAY_OF_MONTH));
		cal.setHour(calendar.get(Calendar.HOUR_OF_DAY));
		cal.setMinute(calendar.get(Calendar.MINUTE));
		cal.setSecond(calendar.get(Calendar.SECOND));
		cal.setMillisecond(calendar.get(Calendar.MILLISECOND));
		cal.setTimezone(calendar.get(Calendar.ZONE_OFFSET) / 60000);

		return cal;

	}*/
	public static int getDaysBetween (Date date1, Date date2){
		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();
		calendar1.setTime(date1);
		calendar2.setTime(date2);
        if (calendar1.after(calendar2)){
            java.util.Calendar swap = calendar1;
            calendar1 = calendar2;
            calendar2 = swap;
        }
        int days = calendar2.get(Calendar.DAY_OF_YEAR) - calendar1.get(Calendar.DAY_OF_YEAR);
        int y2 = calendar2.get(Calendar.YEAR);
        if (calendar1.get(Calendar.YEAR) != y2){
        	calendar1 = (Calendar) calendar1.clone();
            do{
                days += calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);//得到当年的实际天数
                calendar1.add(Calendar.YEAR, 1);
            } while (calendar1.get(Calendar.YEAR) != y2);
        }
        return days;
    }

	public static void main(String[] agrgs) throws ParseException {
		
		//System.out.println(getXMLDate(new Date() ));
		Date date1 = strToDate("20101120", DATEPATTERN);
		Date date2 = new Date();
		System.out.println(date1);
	}

}
